/*
 * Copyright (c) 2011-2013, Peter Abeles. All Rights Reserved.
 *
 * This file is part of BoofCV (http://boofcv.org).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package boofcv.alg.feature.disparity.impl;

import boofcv.misc.CodeGeneratorBase;

import java.io.FileNotFoundException;

/**
 * @author Peter Abeles
 */
public class GenerateSelectSparseStandardWta extends CodeGeneratorBase {

	String dataAbr;
	String sumType;
	String MAX_VALUE;
	boolean isFloat;

	@Override
	public void generate() throws FileNotFoundException {
		createFile(false);
		createFile(true);
	}

	public void createFile(  boolean isFloat ) throws FileNotFoundException {
		this.isFloat = isFloat;
		if( isFloat ) {
			sumType = "float";
			MAX_VALUE = "Float.MAX_VALUE";
			dataAbr = "F32";
		} else {
			sumType = "int";
			MAX_VALUE = "Integer.MAX_VALUE";
			dataAbr = "S32";
		}

		setOutputFile("ImplSelectSparseStandardWta_"+dataAbr);

		printPreamble();
		printSetTexture();
		printSelect();

		out.println("}");
	}

	private void printPreamble() {
		out.print("import boofcv.alg.feature.disparity.SelectSparseStandardWta;\n" +
				"\n"+
				"/**\n" +
				" * <p>\n" +
				" * Implementation of {@link SelectSparseStandardWta} for score arrays of type "+dataAbr+".\n" +
				" * </p>\n" +
				" *\n" +
				" * <p>\n" +
				" * DO NOT MODIFY. Generated by {@link GenerateSelectSparseStandardWta}.\n" +
				" * </p>\n" +
				" *\n" +
				" * @author Peter Abeles\n" +
				" */\n" +
				"public class "+className+" extends SelectSparseStandardWta<"+sumType+"[]> {\n" +
				"\n");
		if( isFloat ) {
			out.print("\t// texture threshold\n" +
					"\tprotected float textureThreshold;\n");
		} else {
			out.print("\t// texture threshold, use an integer value for speed.\n" +
					"\tprotected int textureThreshold;\n"+
					"\tprotected static final int discretizer = 10000;\n");
		}
		out.print("\n" +
				"\tpublic "+className+"(int maxError, double texture) {\n" +
				"\t\tsuper(maxError,texture);\n" +
				"\t}\n\n");
	}

	private void printSetTexture() {
		if( isFloat ) {
			out.print("\t@Override\n" +
					"\tprotected void setTexture( double texture ) {\n" +
					"\t\tthis.textureThreshold = (float)texture;\n" +
					"\t}\n\n");
		} else {
			out.print("\t@Override\n" +
					"\tprotected void setTexture( double texture ) {\n" +
					"\t\tthis.textureThreshold = (int)(discretizer *texture);\n" +
					"\t}\n\n");
		}
	}

	private void printSelect() {
		out.print("\t@Override\n" +
				"\tpublic boolean select("+sumType+"[] scores, int maxDisparity) {\n" +
				"\t\tint disparity = 0;\n" +
				"\t\t"+sumType+" best = scores[0];\n" +
				"\n" +
				"\t\tfor( int i = 1; i < maxDisparity; i++ ) {\n" +
				"\t\t\tif( scores[i] < best ) {\n" +
				"\t\t\t\tbest = scores[i];\n" +
				"\t\t\t\tdisparity = i;\n" +
				"\t\t\t}\n" +
				"\t\t}\n" +
				"\n" +
				"\t\tif( best > maxError ) {\n" +
				"\t\t\treturn false;\n" +
				"\t\t} else if( textureThreshold > 0 ) {\n" +
				"\t\t\t// find the second best disparity value and exclude its neighbors\n" +
				"\t\t\t"+sumType+" secondBest = "+MAX_VALUE+";\n" +
				"\t\t\tfor( int i = 0; i < disparity-1; i++ ) {\n" +
				"\t\t\t\tif( scores[i] < secondBest )\n" +
				"\t\t\t\t\tsecondBest = scores[i];\n" +
				"\t\t\t}\n" +
				"\t\t\tfor( int i = disparity+2; i < maxDisparity; i++ ) {\n" +
				"\t\t\t\tif( scores[i] < secondBest )\n" +
				"\t\t\t\t\tsecondBest = scores[i];\n" +
				"\t\t\t}\n" +
				"\n" +
				"\t\t\t// similar scores indicate lack of texture\n" +
				"\t\t\t// C = (C2-C1)/C1\n");

		if( isFloat ) {
			out.print("\t\t\tif( secondBest-best <= textureThreshold*best )\n");
		} else {
			out.print("\t\t\tif( discretizer*(secondBest-best) <= textureThreshold*best )\n");
		}

		out.print("\t\t\t\treturn false;\n" +
				"\t\t}\n" +
				"\n" +
				"\t\tthis.disparity = disparity;\n" +
				"\n" +
				"\t\treturn true;\n" +
				"\t}\n\n");
	}

	public static void main( String args[] ) throws FileNotFoundException {
		GenerateSelectSparseStandardWta gen = new GenerateSelectSparseStandardWta();

		gen.generate();
	}
}
